package com.reliable.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionAlertsDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionHandlingDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionHandledDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionAlertsDetailDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionSuccessDetailDTO;
import com.reliable.api.dto.user.ReliableUserLite;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionAlertsParam;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionHandlingParam;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionHandledParam;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.utils.StringUtils;
import com.reliable.framework.utils.JwtUtils;
import com.reliable.service.domain.ReliableJurisdictionAlerts;

import com.reliable.service.service.IReliableJurisdictionAlertsService;
import com.reliable.service.utils.IdEncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 属地研判主表 Controller
 *
 * @author ruoyi
 * @date 2025-11-26
 */
@RestController
@RequestMapping("/reliable/jurisdiction/alerts")
public class ReliableJurisdictionAlertsController {

    @Autowired
    private IReliableJurisdictionAlertsService reliableJurisdictionAlertsService;
    
    /**
     * 查询属地研判列表
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ReliableJurisdictionAlertsParam param) {
        ReliableUserLite currentUser = JwtUtils.getCurrentUser();
        if (currentUser != null && currentUser.getCity() != null) {
            String city = currentUser.getCity();
            // 如果用户所在城市名以"市"结尾，则同时添加不带"市"的版本
            if (city.endsWith("市")) {
                param.setCityName(city.substring(0, city.length() - 1)); // 不带"市"的版本
            } else {
                param.setCityName(city);
            }
        }
        IPage<ReliableJurisdictionAlertsDTO> list = reliableJurisdictionAlertsService.selectJurisdictionAlertsList(param);
        list.getRecords().forEach(item -> {
            item.setBusinessId(IdEncryptUtils.encrypt(item.getId()));
            item.setId(null);
        });
        return AjaxResult.success(list);
    }


    /**
     * 获取属地研判详细信息
     */
    @PostMapping("/detail")
    public AjaxResult detail(@RequestBody IdParam param) {
        if (StringUtils.isEmpty(param.getBusinessId())) {
            return AjaxResult.warn("参数错误");
        }
        Long id = IdEncryptUtils.decrypt(param.getBusinessId());
        ReliableJurisdictionAlertsDetailDTO detail = reliableJurisdictionAlertsService.selectJurisdictionAlertsDetail(id);
        return AjaxResult.success(detail);
    }


    /**
     * 处置中列表
     */
    @PostMapping("/handling/list")
    public AjaxResult handlingList(@RequestBody ReliableJurisdictionHandlingParam param) {
        ReliableUserLite currentUser = JwtUtils.getCurrentUser();
        if (currentUser != null && currentUser.getCity() != null) {
            String city = currentUser.getCity();
            // 如果用户所在城市名以"市"结尾，则同时添加不带"市"的版本
            if (city.endsWith("市")) {
                param.setCityName(city.substring(0, city.length() - 1)); // 不带"市"的版本
            } else {
                param.setCityName(city);
            }
        }
        IPage<ReliableJurisdictionHandlingDTO> list = reliableJurisdictionAlertsService.selectJurisdictionHandlingList(param);
        list.getRecords().forEach(item -> {
            item.setBusinessId(IdEncryptUtils.encrypt(item.getId()));
            item.setId(null);
        });
        return AjaxResult.success(list);
    }

    /**
     * 完成处置列表
     */
    @PostMapping("/handled/list")
    public AjaxResult handledList(@RequestBody ReliableJurisdictionHandledParam param) {
        ReliableUserLite currentUser = JwtUtils.getCurrentUser();
        if (currentUser != null && currentUser.getCity() != null) {
            String city = currentUser.getCity();
            // 如果用户所在城市名以"市"结尾，则同时添加不带"市"的版本
            if (city.endsWith("市")) {
                param.setCityName(city.substring(0, city.length() - 1)); // 不带"市"的版本
            } else {
                param.setCityName(city);
            }
        }
        IPage<ReliableJurisdictionHandledDTO> list = reliableJurisdictionAlertsService.selectJurisdictionHandledList(param);
        list.getRecords().forEach(item -> {
            item.setBusinessId(IdEncryptUtils.encrypt(item.getId()));
            item.setId(null);
        });
        return AjaxResult.success(list);
    }

    /**
     * 处置成功详情
     */
    @PostMapping("/success/detail")
    public AjaxResult successDetail(@RequestBody IdParam param) {
        if (StringUtils.isEmpty(param.getBusinessId())) {
            return AjaxResult.warn("参数错误");
        }
        Long id = IdEncryptUtils.decrypt(param.getBusinessId());
        ReliableJurisdictionSuccessDetailDTO detail = reliableJurisdictionAlertsService.selectJurisdictionSuccessDetail(id);
        return AjaxResult.success(detail);
    }
}